package at.im.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import at.im.utils.Box;
import at.im.utils.Entity;
import at.im.utils.EntityState;
import at.im.utils.Game;
import at.im.utils.GameBox;
import at.im.utils.GameBoxLayer;
import at.im.utils.GameBoxState;
import at.im.utils.GameEntity;
import at.im.utils.Sprite;
import at.im.utils.Tileset;
import at.im.utils.User;

public class SQLWrite 
{
	
	public static void write(Connection con, Object object, int type)
	{
		switch(type)
		{
			case 0: 	writeToTable(con, (Box) object); 				break;
			case 1: 	writeToTable(con, (Entity) object); 			break;
			case 2: 	writeToTable(con, (EntityState) object); 		break;
			case 3: 	writeToTable(con, (Game) object); 				break;
			case 4: 	writeToTable(con, (GameBox) object); 			break;
			case 5: 	writeToTable(con, (GameBoxLayer) object); 		break;
			case 6: 	writeToTable(con, (GameBoxState) object);		break;	
			case 7: 	writeToTable(con, (GameEntity) object);			break;
			case 8: 	writeToTable(con, (Sprite) object);				break;
			case 9: 	writeToTable(con, (Tileset) object);			break;
			case 10:	writeToTable(con, (User) object);				break;
		}
	}
	
	private static void writeToTable(Connection con, Box box)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO box VALUES(?)");
	        pst.setInt(1, box.getId());
	        pst.setInt(2, box.getTilesetId());
	        pst.setInt(3, box.getSpriteId());
	        pst.setString(4, box.getName());
	        pst.setString(5, box.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, Entity entity)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO entity VALUES(?)");
	        pst.setInt(1, entity.getId());
	        pst.setInt(2, entity.getTilesetId());
	        pst.setInt(3, entity.getDefaultState());
	        pst.setString(4, entity.getName());
	        pst.setString(5, entity.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, EntityState entitystate)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO entitystate VALUES(?)");
	        pst.setInt(1, entitystate.getEntityStateId());
	        pst.setInt(2, entitystate.getEntityId());
	        pst.setInt(3, entitystate.getSpriteId());
	        pst.setString(4, entitystate.getName());
	        pst.setBoolean(5, entitystate.isVisible());
	        pst.setString(6, entitystate.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, Game game)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO game VALUES(?)");
	        pst.setInt(1, game.getGameId());
	        pst.setInt(2, game.getUserId());
	        pst.setInt(3, game.getTilesetId());
	        pst.setInt(4, game.getForegroundlayerId());
	        pst.setInt(5, game.getCenterlayoutId());
	        pst.setInt(6, game.getBackgroundlayerId());
	        pst.setString(7, game.getBackgroundImage());
	        pst.setString(8, game.getName());
	        pst.setString(9, game.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, GameBox gamebox)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO gamebox VALUES(?)");
	        pst.setInt(1, gamebox.getGameBoxId());
	        pst.setInt(2, gamebox.getGameBoxLayerId());
	        pst.setInt(3, gamebox.getDefaultState());
	        pst.setString(4, gamebox.getName());
	        pst.setInt(5, gamebox.getPosX());
	        pst.setInt(6, gamebox.getPosY());
	        pst.setString(7, gamebox.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, GameBoxLayer gameboxlayer)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO gameboxstate VALUES(?)");
	        pst.setInt(1, gameboxlayer.getGameboxlayerId());
	        pst.setInt(2, gameboxlayer.getGameId());
	        pst.setString(3, gameboxlayer.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, GameBoxState gameboxstate)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO gameboxstate VALUES(?)");
	        pst.setInt(1, gameboxstate.getGameBoxStateId());
	        pst.setInt(2, gameboxstate.getGameBoxId());
	        pst.setInt(3, gameboxstate.getBoxId());
	        pst.setString(4, gameboxstate.getName());
	        pst.setBoolean(5, gameboxstate.isVisible());
	        pst.setString(7, gameboxstate.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, GameEntity gameentity)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO gameentity VALUES(?)");
	        pst.setInt(1, gameentity.getGameEntityId());
	        pst.setInt(2, gameentity.getGameId());
	        pst.setInt(3, gameentity.getEntityId());
	        pst.setString(4, gameentity.getName());
	        pst.setInt(5, gameentity.getPosX());
	        pst.setInt(6, gameentity.getPosY());
	        pst.setString(7, gameentity.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, Sprite sprite)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO sprite VALUES(?)");
	        pst.setInt(1, sprite.getSpriteId());
	        pst.setInt(2, sprite.getTilesetId());
	        pst.setString(3, sprite.getImage());
	        pst.setInt(4, sprite.getFrames());
	        pst.setInt(5, sprite.getFPS());
	        pst.setString(6, sprite.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	private static void writeToTable(Connection con, Tileset tileset)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO tileset VALUES(?)");
	        pst.setInt(1, tileset.getTilesetId());
	        pst.setInt(2, tileset.getUserId());
	        pst.setString(3, tileset.getName());
	        pst.setBoolean(4, tileset.isPublic());
	        pst.setString(5, tileset.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}

	private static void writeToTable(Connection con, User user)
	{
		PreparedStatement pst;
		try {
			pst = con.prepareStatement("INSERT INTO user VALUES(?)");
	        pst.setInt(1, user.getId());
	        pst.setString(2, user.getName());
	        pst.setString(3, user.getEmail());
	        pst.setString(4, user.getPassword());
	        pst.setString(5, user.getTimeStamp());
	        pst.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}

}
